part of pda.service;

class Depart {
  String id;
  String code;
  String name;
  String grade;
  String nature;
  int acreage; // 是否最末级
  int sellers;
  String state;
  String branchId; // column: branch_id
  int period;

  Depart(
      {this.id,
      this.code,
      this.name,
      this.grade,
      this.nature,
      this.acreage,
      this.sellers,
      this.state,
      this.branchId,
      this.period});

  factory Depart.fromJson(Map<String, dynamic> map) {
    return Depart(
        id: map['id'],
        code: map['code'],
        name: map['name'],
        grade: map['grade'],
        nature: map['nature'],
        acreage: map['acreage'],
        sellers: map['sellers'],
        state: map['state'],
        branchId: map['branchId'],
        period: map['period']);
  }

  static String insertSql() {
    return '''insert into pda_depart (
      id,
      code,
      name ,
      grade,
      nature,
      acreage,
      sellers,
      state,
      branch_id,
      period
    ) values (
      ?, 
      ?,
      ?, 
      ?,
      ?, 
      ?,
      ?, 
      ?,
      ?, 
      ?
    )
    ''';
  }

  List<dynamic> insertSqlArgs() {
    return [
      id,
      code,
      name,
      grade,
      nature,
      acreage,
      sellers,
      state,
      branchId,
      period,
    ];
  }

  static Future<List> getAll([List<String> columns]) async {
    var db = await SqliteHelper.getDatabase();
    var list = await db.query('pda_depart',
        columns: columns ?? ['id', 'code', 'name'], orderBy: 'id asc');
    List<Depart> rtn = [];
    for (var item in list) {
      rtn.add(Depart.fromJson(item));
    }
    return rtn;
  }
}
